/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "image.h"

#include "color.h"

namespace Sirat {

namespace Common {

Image::Image(unsigned int _width,unsigned int _height) :  width(_width), height(_height), filename("") , load(false) {
	pixels = new Color* [_width];
	for(unsigned int i = 0; i < _width; i++)
		pixels[i] = new Color[_height];
}

Image::Image() : width(0), height(0), pixels(0), filename(""), load(true) { }

Image::Image(Color** _pixels,const unsigned int _width,const unsigned int _height) : width(_width), height(_height), filename("") , load(false) {
	pixels = new Color* [width];
	for(unsigned int i = 0; i < width; i++)
		pixels[i] = new Color[height];
	
	for(unsigned int y = 0;y < height; y++)
		for (unsigned int x = 0; x < width; x++)
			pixels[x][y] = _pixels[x][y];
}

void Image::initiate_pixels(const unsigned int _width, const unsigned int _height) {
	if(pixels) {
		for (unsigned int i = 0; i < width; i++)
			delete[] pixels[i];
		delete[] pixels;
	}
	pixels = new Color* [_width];
	for(unsigned int i = 0; i < _width; i++)
		pixels[i] = new Color[_height];
}

Image::Image(const Image& _image) { 
	width = _image.width;
	height = _image.height;
	pixels = new Color* [width];
	for(unsigned int i = 0; i < width; i++)
		pixels[i] = new Color[height];
	for(unsigned int y = 0;y < height; y++)
		for (unsigned int x = 0; x < width; x++)
			pixels[x][y] = _image.pixels[x][y];
	filename = _image.filename;
	load = _image.load;
}

Image::~Image() {
	for (unsigned int i = 0; i < width; i++)
		delete[] pixels[i];
	delete[] pixels;
}

void Image::set_pixel(const unsigned int _px,const unsigned int _py,const Color& _color) {
	if(_px < width && _py < height)
		pixels[_px][_py] = _color;
	// Exception need
}

Color Image::get_pixel(const unsigned int _px,const unsigned int _py) {
	if(_px < width && _py < height)
		return pixels[_px][_py];
	else 
		return Color(0.0,0.0,0.0); 
	// Exception need
}

unsigned int Image::get_width() const { 
	return width; 
}

unsigned int Image::get_height() const { 
	return height; 
}

std::string Image::get_filename() const { 
	return filename; 
}

void Image::set_width(const unsigned int _width) { 
	width = _width; 
}

void Image::set_height(const unsigned int _height) { 
	height = _height; 
}

bool Image::loaded() const {
	return load;
}

Image& Image::operator=(const Image& _image) {
	width = _image.width;
	height = _image.height;
	pixels = new Color* [width];
	for(unsigned int i = 0; i < width; i++)
		pixels[i] = new Color[height];
	for(unsigned int y = 0;y < height; y++)
		for (unsigned int x = 0; x < width; x++)
			pixels[x][y] = _image.pixels[x][y];
	filename = _image.filename;
	load = _image.load;

	return *this;
}

Color** Image::operator+(const Image* const _output) const {
	Color** result_pixels = new Color* [width];
	for(unsigned int i = 0; i < width; i++)
		result_pixels[i] = new Color[height];
	
	for(unsigned int x = 0;x < width; x++)
		for (unsigned int y = 0; y < height; y++)
			result_pixels[x][y] = pixels[x][y] + _output->pixels[x][y];
	return result_pixels;
}

}

}
